namespace Digitalmes.Common.Utils;

/// <summary>
/// PDF 帮助类
/// </summary>
public static class PdfUtils
{
    /// <summary>
    /// 获取pdf文档的页数
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns>-1表示文件不存在</returns>
    public static int GetPdFofPageCount(string filePath)
    {
        if (!File.Exists(filePath)) 
            return -1; // -1表示文件不存在

        using FileStream fs = new(filePath, FileMode.Open, FileAccess.Read);
        StreamReader reader = new(fs);
        //从流的当前位置到末尾读取流
        string pdfText = reader.ReadToEnd();
        Regex rgx = new(@"/Type\s*/Page[^s]");
        MatchCollection matches = rgx.Matches(pdfText);
        return matches.Count;
    }
}
